package core.schedule.snake.schedule.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import core.schedule.snake.data.TaskService;
import core.schedule.snake.data.domain.DsfaTask;
import core.schedule.snake.data.domain.TaskResult;
import core.schedule.snake.schedule.impl.TaskTypeInterface;
import core.tool.log.log4j.EasyLogger;

public class DsfaTaskHelper {
	
	private static List<String> timmerList=new ArrayList<String>();
	public static void timmerBussiness(TaskInfo taskInfo) {
		TaskInfo tempTaskInfo=new TaskInfo();
		tempTaskInfo.setPackagePath(taskInfo.getPackagePath());
		try {
			TaskService taskService = new TaskService();
			List<DsfaTask> needOperateTasks = taskService.getDsfaTaskList();
			if (needOperateTasks != null && needOperateTasks.size() != 0) {
				EasyLogger.debug("需要执行任务数量" + needOperateTasks.size());
			}
			for (DsfaTask dsfaTask : needOperateTasks) {
				tempTaskInfo.setCronExpression(dsfaTask.getExectStr());
				if(dsfaTask.getTimmerType()==2) {
					if(!timmerList.contains(dsfaTask.getTaskName())) {
						QuartzUtil.addJob(dsfaTask,taskInfo);
						timmerList.add(dsfaTask.getTaskName());
					}
					continue;	
				}
				boolean flag = false;
				EasyLogger.info("执行任务名称：" + dsfaTask.getTaskName());
				TaskTypeInterface taskTypeInterface = ScanService.getTaskTypeInterface(dsfaTask, taskInfo);
				if (taskTypeInterface != null) {
					dsfaTask.setTaskStatus(DsfaTask.STATUS_START);
					dsfaTask.setStartTime(new Date());
					dsfaTask.setEndTime(null);
					taskService.updateTask(dsfaTask);
					EasyLogger.info("修改任务状态：正在进行中; 修改任务开始时间："+ dsfaTask.getStartTime());
					EasyLogger.debug("任务实现接口：" + taskTypeInterface.getClass());
					Object object = taskTypeInterface.getTaskConfigInfo(dsfaTask);
					EasyLogger.debug("序列化任务执行参数（Xml---JavaBean）完成");
					TaskResult taskResult = taskTypeInterface.operateTask(dsfaTask, object);
					EasyLogger.info("任务执行结果："+ taskResult.getExecuteDesc());
					if (!taskResult.isExecuteFlag()) {
						flag = true;
						dsfaTask.setTaskStatus(DsfaTask.STATUS_EXCEPTION);
						dsfaTask.setEndTime(new Date());
						dsfaTask.setTaskResult("异常:"+ taskResult.getExecuteDesc());
						taskService.updateTask(dsfaTask);
						EasyLogger.info("修改任务结束时间:" + dsfaTask.getEndTime()+ " 状态为:异常");
					}
					if (flag) continue;
					dsfaTask.setTaskStatus(DsfaTask.STATUS_OVER);
					dsfaTask.setEndTime(new Date());
					dsfaTask.setTaskResult(taskResult.getExecuteDesc());
					taskService.updateTask(dsfaTask);
					EasyLogger.info("修改任务结束时间:" + dsfaTask.getEndTime()+ " 状态为:结束");
				}
			}
		} catch (Exception e) {
			EasyLogger.error(e.toString());
		}			
	}	
	
	
	 public static TaskInfo getTaskInfo() {
			TaskInfo taskInfo=new TaskInfo();
			taskInfo.setImplType(taskInfo.IMPL_TYPE_QUTARZ);
			taskInfo.setInitTime(1);
			taskInfo.setJiangeTime(1);
			taskInfo.setTimeUnit(TimeUnit.SECONDS);
			taskInfo.setPackagePath("apps");
			taskInfo.setCronExpression("* * * * * ?");
			return taskInfo;
	}
	

}
